package cn.flying.cloud.task.core.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import cn.flying.cloud.task.core.quartz.job.ScheduleJobExecution;
import cn.flying.cloud.task.core.quartz.scheduler.ScheduleManager;
import cn.flying.cloud.task.entity.TaskJobInfo;

/**
 * Job任务操作类
 *
 * @author: admin
 * @date: 2024年06月06日 16:10
 * @version: 1.0
 */
@Component
public class JobService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ScheduleManager scheduleManager;
    @Resource
    private ScheduleJobExecution scheduleJobExecution;

    /**
     * 新增任务
     *
     * @param jobInfo
     * @return
     */
    public void startJob(TaskJobInfo jobInfo) {
        scheduleManager.startJob(jobInfo);
    }

    /**
     * 停止、删除任务（表示完全不用了，下线了，如需重新启用，需要使用如下方法重新调度：）
     *
     * @param jobName
     * @param jobGroup
     */

    public void stopJob(String jobName, String jobGroup) {
        scheduleManager.stopJob(jobName, jobGroup);
    }

    /**
     * 暂停任务
     *
     * @param jobName
     * @param jobGroup
     */
    public void pauseJob(String jobName, String jobGroup) {
        scheduleManager.pauseJob(jobName, jobGroup);
    }

    /**
     * 恢复任务
     *
     * @param jobName
     * @param jobGroup
     */
    public void resumeJob(String jobName, String jobGroup) {
        scheduleManager.resumeJob(jobName, jobGroup);
    }

    /**
     * 立即触发执行一次任务
     *
     * @param jobName
     * @param jobGroup
     */
    public void triggerJob(String jobName, String jobGroup, String param) {
        scheduleManager.triggerJob(jobName, jobGroup, param);
    }

    /**
     * 手动补偿处理
     *
     * @param jobInfo
     * @param serialNo
     * @param param
     * @param host
     */
    public void resolveJob(TaskJobInfo jobInfo, String serialNo, String param, String host) {
        scheduleJobExecution.resolveJob(jobInfo, serialNo, param, host);
    }
}
